<?php
class DictsModel extends BaseModel 
{
	private static $MAX_DICT_NAME_LENGTH = 20;
	private static $MIN_DICT_NAME_LENGTH = 1;
	private static $MAX_DICT_DESCR_LENGTH = 120;
	private static $MIN_DICT_DESCR_LENGTH = 1;
	
	public function fetchUserDicts()	
	{
		$user_id = $this->getUserIdOrZero();
		return DbCommunicator::fetchUserDicts($this->pdo, $user_id);
	}
	
	public function fetchAllDicts()
	{
		$user_id = $this->getUserIdOrZero();
		// user_id negali būti nulis, nes vartotojas autorizuotas
		return DbCommunicator::fetchAllDictsData($this->pdo, $user_id);
	}
	
	public function unsubscribeDict($dict_id) 
	{
		$user_id = $this->getUserIdOrZero();
		
		$success = DbCommunicator::unsubscribeDict($this->pdo, $user_id, $dict_id);
		
		if ($success) 
		{
			$result['messages'][] = "Atsijungta nuo žodyno";
			$result['success'] = true;
			if (!DbCommunicator::dictIsInUse($this->pdo, $dict_id))
			{
				$deleteItemsResult = DbCommunicator::deleteItems($this->pdo, $dict_id);
				if ($deleteItemsResult) {
					$result['messages'][] = "Kadangi žodyno niekas nebenaudoja, visi jo žodžiai ištrinti";
				}
				else
				{
					$result['messages'][] = "Žodyno niekas nebenaudoja, bet žodžių ištrinti nepavyko";
				}
			}					
		}
		else 
		{
			$result['messages'][] = "Atsijungti nepavyko";
			$result['success'] = false;
		}		

		return $result;
	}
	
	public function subscribeDict($dict_id) 
	{
		$user_id = $this->getUserIdOrZero();
		
		if (!DbCommunicator::isSubscribed($this->pdo, $user_id, $dict_id))
		{
			if (DbCommunicator::subscribeDict($this->pdo, $user_id, $dict_id, $managed))
			{
				$result['messages'][] = "Žodynas prisijungtas";
				$result['success'] = true;		
			}
			else 
			{
				$result['messages'][] = "Žodyno prisijungti nepavyko";
				$result['success'] = false;					
			}
		}
		else 
		{
			$result['messages'][] = "Žodynas jau buvo prisijungtas";
			$result['success'] = true;
		}
		
		return $result;
	}
	
	public function createNewDict($name, $description) 
	{		
		$result['success'] = true;
		
		$user_id = $this->getUserIdOrZero();
		
		$name = makeSafe($name);
		$description = makeSafe($description);
		
		// tikrinama, ar tinkamas pavadinimas ir aprašymas
		
		if ($name.lenght() > DictsModel::$MAX_DICT_NAME_LENGTH ||
			$name.lenght() < DictsModel::$MIN_DICT_NAME_LENGTH)
		{
			$result['messages'][] = "Netinkamas pavadinimo ilgis";
			$result['success'] = false;
		}
		
		if ($description.lenght() > DictsModel::$MAX_DICT_DESCR_LENGTH ||
			$description.lenght() < DictsModel::$MIN_DICT_DESCR_LENGTH)
		{
			$result['messages'][] = "Netinkamas aprašymo ilgis";
			$result['success'] = false;			
		}
		
		if (!$result['success']) 
		{
			// įdeda papildomą pranešimą į masyvo pradžią
			array_splice($result['messages'], 0, 0, "Žodynas nesukurtas");
			return $result;
		}
		
		// kuriamas naujas žodynas ir surišamas su vartotoju
		
 		$new_id = DbCommunicator::createNewDict($this->pdo, $name, $description, $user_id, true);
		if ($new_id) 
		{
			$result['messages'][] = "Žodynas sukurtas";
			$result['success'] = $new_id;
		}
		else 
		{
			$result['messages'][] = "Žodyno sukurti nepavyko";
			$result['success'] = 0;			
		}
		
		return $result;
	}
	
	public function createDictDuplicate($dict_id) 
	{		
		$result['success'] = true;
		
		$user_id = $this->getUserIdOrZero();
		
		// patikrinti, ar tinkamas $dict_id
		if (!is_int($dict_id) ||
			$dict_id < 1 ||
			!DbCommunicator::dictExists($dict_id))
		{
			$result['messages'][] = "Žodynas nesukurtas";
			$result['success'] = false;
			return $result;
		}
		
		// kuriamas naujas žodynas, surišamas su vartotoju ir duplikuojami jo įrašai
		
 		$new_id = DbCommunicator::createDictDuplicate($this->pdo, $user_id, $dict_id);
		if ($new_id) 
		{
			$result['messages'][] = "Žodynas sukurtas";
			$result['success'] = $new_id;
		}
		else 
		{
			$result['messages'][] = "Žodyno sukurti nepavyko";
			$result['success'] = 0;			
		}
		
		return $result;		
	}
	
}